﻿#include "xmlconfig.h"

#include "sdk/QsLog/include/QsLog.h"

#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;

#include <QDebug>
#include <QApplication>
#include <QFile>

XmlConfig::XmlConfig()
{
    // 读取XML配置文件
    QString filename = QString::fromUtf8("%1/%2.xml").arg(qApp->applicationDirPath(), qApp->applicationName());
    readConfig(filename);
}

bool XmlConfig::containFlow(const int &level) const
{
    return m_flowMap.contains(level);
}

Flow XmlConfig::flow(const int &level) const
{
    return m_flowMap.value(level);
}

QList<Subflow> XmlConfig::subflow0List(const int &level) const
{
    QList<Subflow> subflow0List;
    QList<int> keys = m_subflow0Map.keys();
    for(int i = 0; i < keys.size(); i++) {
        int key = keys.at(i);
        if (QString::number(key).startsWith(QString::number(level)))
            subflow0List << m_subflow0Map.value(key);
    }

    return subflow0List;
}

QList<Subflow> XmlConfig::subflow1List(const int &subflow0) const
{
    QList<Subflow> subflow1List;
    QList<int> keys = m_subflow1Map.keys();
    for(int i = 0; i < keys.size(); i++) {
        int key = keys.at(i);
        if (QString::number(key).startsWith(QString::number(subflow0)))
            subflow1List << m_subflow1Map.value(key);
    }

    return subflow1List;
}

QList<Subflow> XmlConfig::subflow2List(const int &subflow1) const
{
    QList<Subflow> subflow2List;
    QList<int> keys = m_subflow2Map.keys();
    for(int i = 0; i < keys.size(); i++) {
        int key = keys.at(i);
        if (QString::number(key).startsWith(QString::number(subflow1)))
            subflow2List << m_subflow2Map.value(key);
    }

    return subflow2List;
}

QList<Subflow> XmlConfig::subflow3List(const int &subflow2) const
{
    QList<Subflow> subflow3List;
    QList<int> keys = m_subflow3Map.keys();
    for(int i = 0; i < keys.size(); i++) {
        int key = keys.at(i);
        if (QString::number(key).startsWith(QString::number(subflow2)))
            subflow3List << m_subflow3Map.value(key);
    }

    return subflow3List;
}

bool XmlConfig::containOperation(const int &operationId) const
{
    return m_operationMap.contains(operationId);
}

QList<Step> XmlConfig::stepList(const int &operationId) const
{
    return m_operationMap.value(operationId);
}

void XmlConfig::readConfig(const QString &filename)
{
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError ret = doc.LoadFile(filename.toLocal8Bit().constData());
    if (ret) {
        QLOG_ERROR() << "XML: " << QString("fail to open the file: %1, %2, %3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__);
        return;
    }

    // 根节点
    XMLElement* rootNode = doc.RootElement();
    // flow节点
    XMLElement* flowNode = rootNode->FirstChildElement("flow");
    while (flowNode) {
        Flow flow;
        flow.level = QString::fromUtf8(flowNode->Attribute("level")).toInt();
        flow.name = QString::fromUtf8(flowNode->Attribute("name"));
        m_flowMap.insert(flow.level, flow);

        // subflow0节点
        XMLElement* subflow0Node = flowNode->FirstChildElement("subflow0");
        while (subflow0Node) {
            Subflow subflow0;
            subflow0.id = QString::fromUtf8(subflow0Node->Attribute("id")).toInt();
            subflow0.name = QString::fromUtf8(subflow0Node->Attribute("name"));
            subflow0.operation = QString::fromUtf8(subflow0Node->Attribute("operation")).toInt();
            m_subflow0Map.insert(subflow0.id, subflow0);

            // subflow1节点
            XMLElement* subflow1Node = subflow0Node->FirstChildElement("subflow1");
            while (subflow1Node) {
                Subflow subflow1;
                subflow1.id = QString::fromUtf8(subflow1Node->Attribute("id")).toInt();
                subflow1.name = QString::fromUtf8(subflow1Node->Attribute("name"));
                subflow1.operation = QString::fromUtf8(subflow1Node->Attribute("operation")).toInt();
                m_subflow1Map.insert(subflow1.id, subflow1);

                // subflow2节点
                XMLElement* subflow2Node = subflow1Node->FirstChildElement("subflow2");
                while (subflow2Node) {
                    Subflow subflow2;
                    subflow2.id = QString::fromUtf8(subflow2Node->Attribute("id")).toInt();
                    subflow2.name = QString::fromUtf8(subflow2Node->Attribute("name"));
                    subflow2.operation = QString::fromUtf8(subflow2Node->Attribute("operation")).toInt();
                    m_subflow2Map.insert(subflow2.id, subflow2);

                    // subflow3节点
                    XMLElement* subflow3Node = subflow2Node->FirstChildElement("subflow3");
                    while (subflow3Node) {
                        Subflow subflow3;
                        subflow3.id = QString::fromUtf8(subflow3Node->Attribute("id")).toInt();
                        subflow3.name = QString::fromUtf8(subflow3Node->Attribute("name"));
                        subflow3.operation = QString::fromUtf8(subflow3Node->Attribute("operation")).toInt();
                        m_subflow3Map.insert(subflow3.id, subflow3);

                        // 下一个兄弟节点: subflow3Node
                        subflow3Node = subflow3Node->NextSiblingElement();
                    }

                    // 下一个兄弟节点: subflow2Node
                    subflow2Node = subflow2Node->NextSiblingElement();
                }

                // 下一个兄弟节点: subflow1Node
                subflow1Node = subflow1Node->NextSiblingElement();
            }

            // 下一个兄弟节点: subflow0Node
            subflow0Node = subflow0Node->NextSiblingElement();
        }

        // 下一个兄弟节点: flowNode
        flowNode = flowNode->NextSiblingElement();
    }

    // operations节点
    XMLElement* operationsNode = rootNode->FirstChildElement("operations");
    if (operationsNode) {
        // operation节点
        XMLElement* operationNode = operationsNode->FirstChildElement("operation");
        while (operationNode) {
            int operationId = QString::fromUtf8(operationNode->Attribute("id")).toInt();
            //QString operationName = QString::fromUtf8(operationNode->Attribute("name"));

            // step节点
            QList<Step> stepList;
            XMLElement* stepNode = operationNode->FirstChildElement("step");
            while (stepNode) {
                Step step;
                step.id = QString::fromUtf8(stepNode->Attribute("id")).toInt();
                step.timeline = QString::fromUtf8(stepNode->Attribute("timeline")).toInt();
                step.name = QString::fromUtf8(stepNode->Attribute("name"));
                step.code = QString::fromUtf8(stepNode->Attribute("code"));
                step.send = QString::fromUtf8(stepNode->Attribute("send")).toInt() != 0;
                step.wait = QString::fromUtf8(stepNode->Attribute("wait")).toInt() != 0;
                step.video = QString::fromUtf8(stepNode->Attribute("video"));
                stepList << step;

                // 下一个兄弟节点: stepNode
                stepNode = stepNode->NextSiblingElement();
            }
            m_operationMap.insert(operationId, stepList);

            // 下一个兄弟节点: operationNode
            operationNode = operationNode->NextSiblingElement();
        }
    }
}
